package com.financing.wap.service.impl;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.financing.mail.dto.MailContentDTO;
import com.financing.mail.service.MailSenderService;
import com.financing.mail.service.impl.MailSenderServiceImpl;
import com.financing.wap.web.controller.BaseController;
/**
 * @author CUIMO@superfa.cn
 * @version 2016年1月8日 上午11:57:40
 * @Description
 * 
 */
@Service("EmailSendServiceImp")
public class EmailSendServiceImp extends MailSenderServiceImpl  {
	/**
	 * 日志类
	 */
	protected static Logger log = LoggerFactory.getLogger(BaseController.class);
	
	@Resource
	private MailSenderServiceImpl mailSenderService;
	
	String from = "server@superfa.cn";
	@Override
	public boolean send(MailContentDTO request) {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		MimeMessage mailMessage = senderImpl.createMimeMessage();
		try {
			MimeMessageHelper messageHelper = new MimeMessageHelper(
					mailMessage, true, "utf-8");
			messageHelper.setTo(request.getRecAddress().split(";"));
			messageHelper.setFrom(this.from);
			messageHelper.setSubject(request.getSubject());
			messageHelper.setText(request.getContext(), true);
			String cc = request.getCc();
			if (StringUtils.isNotEmpty(cc)) {
				messageHelper.setCc(cc.split(";"));
			}
			this.mailSenderService.getJavaMailSender().send(mailMessage);
		} catch (MessagingException e) {
			log.error("send email error", e);
			return false;
		}
		return true;
	}
	
}
